/**
 * 
 */
package template.page.admin;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.AbstractLink;
import org.apache.click.control.ActionButton;
import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;

import template.page.BorderPage;

/**
 * @author jaracje
 *
 */
@SuppressWarnings("serial")
public class ManagePlayers extends BorderPage {

	public String title = "Manage Players";
	
	private Table playerTable = new Table("playerTable");
	private PageLink editPlayerLink = new PageLink("Edit",EditPlayer.class);
	private ActionButton createPlayerButton = new ActionButton("addPlayerButton","Add Player");
	private ActionLink deletePlayerLink = new ActionLink("Delete",this,"onDeletePlayerClick");
	
	@SuppressWarnings("rawtypes")
	public ManagePlayers() 
	{
		addControl(playerTable);
		addControl(editPlayerLink);
		addControl(deletePlayerLink);
		addControl(createPlayerButton);
		
		// Setup table
		playerTable.setClass(Table.CLASS_ITS);		
		playerTable.setShowBanner(false);
		playerTable.setSortable(true);

		playerTable.addColumn(new Column("id"));
		playerTable.addColumn(new Column("name"));
		playerTable.addColumn(new Column("email"));
        
        editPlayerLink.setImageSrc("/assets/images/actionEdit.gif");
        editPlayerLink.setTitle("Edit player");
        
        createPlayerButton.setTitle("Create player");
        createPlayerButton.setListener(this, "onCreatePlayerClick");
        
        deletePlayerLink.setImageSrc("/assets/images/actionDelete.gif");
        deletePlayerLink.setTitle("Delete player");
        deletePlayerLink.setAttribute("onclick",
            "return window.confirm('Are you sure you want to delete this record?');");

        Column column = new Column("Action");
        column.setTextAlign("center");
        AbstractLink[] links = new AbstractLink[] { editPlayerLink, deletePlayerLink };
        column.setDecorator(new LinkDecorator(playerTable, links, "id"));
        column.setSortable(false);
        playerTable.addColumn(column);

        // Table rowList will be populated through a DataProvider which loads
        // data on demand.
        playerTable.setDataProvider(new DataProvider() {

            public List getData() {
                return getGameSessionService().getAllPlayers();
            }
        });

        // Below we setup the table to preserve it's state (sorting and paging)
        // while editing customers
        playerTable.getControlLink().setActionListener(new ActionListener() {

            public boolean onAction(Control source) {
                // Save Table sort and paging state between requests.
                // NOTE: we set the listener on the table's Link control which is invoked
                // when the Link is clicked, such as when paging or sorting.
                // This ensures the table state is only saved when the state changes, and
                // cuts down on unnecessary session replication in a cluster environment.
            	playerTable.saveState(getContext()); 
                return true;
            }
        });

        // Restore the table sort and paging state from the session between requests
        playerTable.restoreState(getContext()); 
	}
	
	/**
     * Handle the delete row click event.
     */    
    public boolean onDeletePlayerClick() {
        Long id = deletePlayerLink.getValueLong();
        getGameSessionService().deletePlayer(id);
        return true;
    }
    
	/**
     * Handle the delete row click event.
     */    
    public boolean onCreatePlayerClick() {
    	setRedirect(EditPlayer.class);        
        return true;
    }

}
